{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import traceback\n",
    "import shutil\n",
    "def recursive_mv(fileDir, newDir, sufix='jpg'):\n",
    "    currIndex = 1\n",
    "    def get_file_name(currIndex):\n",
    "        res = str(currIndex)\n",
    "        for i in range(0, 5):\n",
    "            if len(res) == 6:\n",
    "                return res\n",
    "            res = '0' + res\n",
    "        return res\n",
    "    try:\n",
    "        if not os.path.exists(newDir):\n",
    "            os.mkdir(newDir)\n",
    "        else:\n",
    "            file = os.listdir(newDir)\n",
    "            if len(file) > 0:\n",
    "                print(\"Target directory is not empty! Please select an empty folder!\")\n",
    "        for parent, dirnames, filenames in os.walk(fileDir):\n",
    "            for dirname in dirnames:\n",
    "                print(\"In moving Dir:\", dirname)\n",
    "                for ele in os.listdir(fileDir + '/' + dirname):\n",
    "                    from_file = fileDir + '/' + dirname + '/' + ele\n",
    "                    to_file = newDir + '/' + get_file_name(currIndex) + '.' + str(sufix)\n",
    "                    shutil.copyfile(from_file, to_file)\n",
    "                    print('Move:',  from_file, ' to ', to_file)\n",
    "                    currIndex = currIndex + 1\n",
    "    except Exception as e:\n",
    "        traceback.print_exc(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In moving Dir: pic1\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic1/drone.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000001.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic1/start_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000002.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic1/stop_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000003.jpg\n",
      "In moving Dir: pic2\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic2/drone.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000004.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic2/start_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000005.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic2/stop_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000006.jpg\n",
      "In moving Dir: pic3\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic3/drone.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000007.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic3/start_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000008.jpg\n",
      "Move: C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train/pic3/stop_col.jpg  to  C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test/000009.jpg\n"
     ]
    }
   ],
   "source": [
    "newDir = 'C:/Users/hp/Documents/Python Scripts/excises/train_test_script/test'\n",
    "recursive_mv('C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train', newDir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n",
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n",
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n"
     ]
    }
   ],
   "source": [
    "for ele in os.listdir('C:/Users/hp/Documents/Python Scripts/excises/train_test_script/train'):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
